import unittest

import requests
from parameterized import parameterized

from api import log
from api.api_register_login import ApiRegisterLogin
from api.api_tender import ApiTender
from util import parser_html, read_json


class TestTender(unittest.TestCase):
    # 初始化
    def setUp(self) -> None:
        # 获取session对象
        self.session = requests.session()
        # 获取ApiTender实例对象
        self.tender = ApiTender(self.session)
        # 调用登录
        ApiRegisterLogin(self.session).api_login()

    # 结束
    def tearDown(self) -> None:
        # 关闭session
        self.session.close()

    @parameterized.expand(read_json("tender.json","tender"))
    # 1、后台投资接口
    def test01_tender(self, amount,expect_text):
        try:
            # 1、调用后台投资接口
            r = self.tender.api_tender(amount)
            log.info("执行后台投资接口状态为：{}".format(r.text))
            if amount == 100:
                # 2、调用三方投资
                result = parser_html(r)
                # 期望 (http:xxx,{"Version":10,})
                r = self.session.post(url=result[0], data=result[1])
                print("三方投资的结果为:", r.text)
                self.assertIn("OK", r.text)
                log.info("接口执行的结果为：{}".format(r.text))
                # 2、查看响应信息
                self.assertIn(expect_text, r.text)
                log.info("执行断言成功通过！")
            else:
                self.assertIn(expect_text,r.text)
        except Exception as e:
            log.error("执行断言错误的原因为：{}".format(e))
            raise
